<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of client
 *
 * @author Skaye
 */
class client {        
    
    private $id;
    private $nom;
    private $prenom;
    private $rue;
    private $cp;
    private $ville;
    private $email;
    private $tel;
    private $login;
    private $mdp;
    private $laCommande;
    private $lesCommandes;
    private $lePanier;
    private $nbCommandes;

    //constructeur
    function __construct($nom, $prenom, $email, $rue, $cp, $ville, $tel, $login, $mdp) {
        $this->email = $email;
        $this->nom = $nom;
        $this->prenom = $prenom;
        $this->rue = $rue;
        $this->cp = $cp;
        $this->ville = $ville;
        $this->tel = $tel;
        $this->login = $login;
        $this->mdp = $mdp;
        $this->lesCommandes = Array();
        $this->nbCommandes = 0;
    }

    //accesseurs
    public function setId($id) {
        $this->id = $id;
    }

    public function getId() {
        return $this->id;
    }

    public function getLaCommande() {
        return $this->laCommande;
    }

    public function setLaCommande($laCommande) {
        $this->laCommande = $laCommande;
    }

    public function getEmail() {
        return $this->email;
    }

    public function getNbCommandes() {
        return $this->nbCommandes;
    }

    public function getNom() {
        return $this->nom;
    }

    public function getPrenom() {
        return $this->prenom;
    }

    public function getRue() {
        return $this->rue;
    }

    public function getCp() {
        return $this->cp;
    }

    public function getVille() {
        return $this->ville;
    }

    public function getTel() {
        return $this->tel;
    }

    public function getLogin() {
        return $this->login;
    }

    public function getMdp() {
        return $this->mdp;
    }

    public function getLesCommandes() {
        return $this->lesCommandes;
    }

    public function getLePanier() {
        return $this->lePanier;
    }

    //modificateurs
    public function setNom($nom) {
        $this->nom = $nom;
    }

    public function setPrenom($prenom) {
        $this->prenom = $prenom;
    }

    public function setRue($rue) {
        $this->rue = $rue;
    }

    public function setCp($cp) {
        $this->cp = $cp;
    }

    public function setVille($ville) {
        $this->ville = $ville;
    }

    public function setTel($tel) {
        $this->tel = $tel;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function setNbCommandes($nbCommandes) {
        $this->nbCommandes = $nbCommandes;
    }

    public function setLogin($login) {
        $this->login = $login;
    }

    public function setMdp($mdp) {
        $this->mdp = $mdp;
    }

    public function setLesCommandes($lesCommandes) {
        $this->lesCommandes = $lesCommandes;
    }
 
    public function setLePanier($lePanier) {
        $this->lePanier = $lePanier;
    }
    
    public function ajoutCommande($uneCommande) {
        $this->nbCommandes = $this->nbCommandes + 1;
        $this->lesCommandes[$this->nbCommandes] = $uneCommande;
    }

    public function ajoutLePanier($lePanier) {
        $this->lePanier = $lePanier;
    }

    public function chargerLePanier(Bdd $sql) {

        $reqPanier = "SELECT pan_num, pan_montant, pan_nbProduit
                      FROM panier, client 
                      WHERE cli_panier = pan_num 
                      AND cli_id =" . $this->id;

        $lesPaniers = $sql->query($reqPanier);

        foreach($lesPaniers as $unPanier){
            $lePanier = new Panier($unPanier['pan_montant'], $unPanier['pan_nbProduit']);
            $lePanier->setId($unPanier['pan_num']);
            $this->lePanier = $lePanier;
        }
    }

    public function chargerLesCommandes(Bdd $sql) {
        $reqCommandes = "SELECT com_id, com_tva, com_ht 
                         FROM commande, client 
                         WHERE com_client = cli_id 
                         AND cli_id =" . $this->id;
        $lesCmd = $sql->query($reqCommandes);
        if ($this->nbCommandes == 0) {
            foreach ($lesCmd as $laCmd) {
                $uneCmd = new Commande($laCmd['com_id'],
                                $laCmd['com_tva'],
                                $laCmd['com_ht']);

                $this->ajoutCommande($uneCmd);
            }
        }
    }

    public function chargerLaCommande(Bdd $sql, $id) {
        $reqCommandes = "SELECT com_id, com_tva, com_ht 
                         FROM commande 
                         WHERE com_id =" . $id;
        $resultCmd = $sql->query($reqCommandes);
        foreach ($resultCmd as $laCmd) {
            $uneCmd = new Commande($laCmd['com_id'],
                            $laCmd['com_tva'],
                            $laCmd['com_ht']);

            $this->setLaCommande($uneCmd);
        }
    }

    public function modifierClient(Bdd $sql) {
        $maj = "UPDATE client
                SET cli_nom     ='$this->nom',
                    cli_prenom  ='$this->prenom',
                    cli_rue     ='$this->rue',
                    cli_cp      ='$this->cp',
                    cli_ville   ='$this->ville',
                    cli_tel     ='$this->tel',
                    cli_email    ='$this->email',
                    cli_login   ='$this->login',
                    cli_mdp     ='$this->mdp'
                WHERE cli_id    = $this->id";
        $sql->query($maj);
    }

    public function ajoutClient(Bdd $sql) {
        // Gestion de l'auto incrémentation pour l'id du client
        $req = "SELECT MAX(cli_id) as max FROM client";
        $nb = $sql->query($req)->fetchColumn();
        $nb++;

        $this->id = $nb;
        
        // Gestion de l'auto incrémentation pour l'id du panier
        $reqPanier = "SELECT MAX(pan_num) as max FROM panier";
        $numPanier = $sql->query($reqPanier)->fetchColumn();
        $numPanier++;
        
        $unPanier = new Panier();
        $this->lePanier = $unPanier;
        
        $unPanier->ajoutPanier($sql); 
        
        $new = "INSERT INTO client
                VALUES ('$this->id',
                        '$numPanier',
                        '$this->nom',
                        '$this->prenom',
                        '$this->rue',
                        '$this->cp',
                        '$this->ville',
                        '$this->tel',
                        '$this->email',
                        '$this->login',
                        '$this->mdp')";

        $sql->query($new);      
    }

    public function supprimerPanier(Bdd $sql) {
        $lePanier = $this->lePanier;
        $idPanier = $lePanier->getId();
        
        $req = "UPDATE client
                SET cli_panier = 0
                WHERE cli_id = $this->id";
        $sql->query($req);
        
        $del = "DELETE FROM panier
                WHERE pan_num = '$idPanier'";

        $sql->query($del);
    }
    
    public function supprimerClient(Bdd $sql) {
        $del = "DELETE FROM client
                WHERE cli_id = '$this->id'";

        $sql->query($del);
    }

}
?>

